//
//  Setting.m
//  RRQS
//
//  Created by kyjun on 16/3/29.
//
//

#import "Setting.h"
#import "JPUSHService.h"
#import <SVWebViewController/SVWebViewController.h>

@interface Setting ()

@property(nonatomic,strong) UIView* footerView;
@property(nonatomic,strong) UIButton* btnLoginOut;

@property(nonatomic,strong) NSString* cellIdentifier;

@end

@implementation Setting

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"设置";
    self.cellIdentifier = @"UITableViewCell";
    
    [self layoutUI];
    [self layoutConstraints];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark =====================================================  user interface layout
-(void)layoutUI{
    self.footerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 150.f);
    [self.footerView addSubview:self.btnLoginOut];
    self.tableView.tableFooterView = self.footerView;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:self.cellIdentifier];
}

-(void)layoutConstraints{
    self.btnLoginOut.translatesAutoresizingMaskIntoConstraints = NO;
    
    [self.btnLoginOut addConstraint:[NSLayoutConstraint constraintWithItem:self.btnLoginOut attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:SCREEN_WIDTH-20]];
    [self.btnLoginOut addConstraint:[NSLayoutConstraint constraintWithItem:self.btnLoginOut attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:40.f]];
    [self.footerView addConstraint:[NSLayoutConstraint constraintWithItem:self.btnLoginOut attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.footerView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.f]];
    [self.footerView addConstraint:[NSLayoutConstraint constraintWithItem:self.btnLoginOut attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.footerView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.f]];
}

#pragma mark =====================================================  <UITableViewDataSource>
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier forIndexPath:indexPath];
    if(indexPath.row == 0){
        cell.textLabel.text = @"管理细则";
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }else if (indexPath.row == 1){
        cell.textLabel.text = @"版本号";
        UILabel* label = [[UILabel alloc]init];
        label.textAlignment = NSTextAlignmentRight;
        label.frame = CGRectMake(SCREEN_WIDTH-SCREEN_WIDTH/3-10, 0, SCREEN_WIDTH/3, 40.f);
        NSDictionary *bundleDic = [[NSBundle mainBundle] infoDictionary];
        NSString* releaseVersion = [bundleDic objectForKey:@"CFBundleShortVersionString"];
        label.text = [NSString stringWithFormat:@"V%@",releaseVersion];
        [cell.contentView addSubview:label];
        
    }
    return cell;
}

#pragma mark =====================================================  <UITableViewDelegate>
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40.f;
}

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == 0){
        return YES;
    }
    return  NO;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSURL *URL = [NSURL URLWithString:@"http://www.xyrr.com/mobile/help"];
    SVWebViewController *webViewController = [[SVWebViewController alloc] initWithURL:URL];
    [self.navigationController pushViewController:webViewController animated:YES];
}


#pragma mark =====================================================  SEL
-(IBAction)loginOutTouch:(UIButton*)sender{
    MMember* entity = self.Identity.userInfo;
    entity.isLogin = NO;
    Boolean flag= [NSKeyedArchiver archiveRootObject:entity toFile:[WMHelper archiverPath]];
    if(flag){
        [self logoutJPush:entity.userID];
        [self.navigationController popToRootViewControllerAnimated:YES];
    }else{
        
    }
}

#pragma mark =====================================================  private method
-(void)logoutJPush:(NSString*)userID{
    __autoreleasing NSMutableSet *tags = [NSMutableSet set];
    __autoreleasing NSString *alias = @"";
    
    [JPUSHService setTags:tags alias:alias fetchCompletionHandle:^(int iResCode, NSSet *iTags, NSString *iAlias) {
        NSLog(@"TagsAlias回调: iResCode:%d  iAlias:%@",iResCode,iAlias);
        if(iResCode == 0){
            NSLog( @"====================");
        }else{
            UIAlertView* alert = [[UIAlertView alloc]initWithTitle:nil message:@"消息推送退出失败!" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alert show];
        }
    }];
}

#pragma mark =====================================================  property package

-(UIView *)footerView{
    if(!_footerView){
        _footerView = [[UIView alloc]init];
    }
    return _footerView;
}

-(UIButton *)btnLoginOut{
    if(!_btnLoginOut){
        _btnLoginOut = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnLoginOut.backgroundColor = [UIColor colorWithRed:213/255.f green:213/255.f blue:213/255.f alpha:1.0];
        [_btnLoginOut setTitleColor:[UIColor colorWithRed:109/255.f green:109/255.f blue:109/255.f alpha:1.0] forState:UIControlStateNormal];
        [_btnLoginOut setTitle:@"退出" forState:UIControlStateNormal];
        _btnLoginOut.layer.masksToBounds = YES;
        _btnLoginOut.layer.cornerRadius = 5.f;
        [_btnLoginOut addTarget:self action:@selector(loginOutTouch:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _btnLoginOut;
}
@end
